﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Commons;
using WHC.OrderWater.Business;
using WHC.Pager.Entity;

namespace WHC.Hotel.UI
{
    public partial class FrmProductType : BaseForm
    {
        public FrmProductType()
        {
            InitializeComponent();
        }

        public override void RefreshForm()
        {
            BindData();
        }

        private void FrmCustomerArea_Load(object sender, EventArgs e)
        {
            BindData();

            this.winGridViewPager1.ProgressBar = this.toolStripProgressBar1.ProgressBar;
            this.winGridViewPager1.OnPageChanged += new EventHandler(winGridViewPager1_OnPageChanged);
            this.winGridViewPager1.OnStartExport += new EventHandler(winGridViewPager1_OnStartExport);
            this.winGridViewPager1.OnEditSelected += new EventHandler(winGridViewPager1_OnEditSelected);
            if (Portal.gc.HasFunction("BasicInfo/ProductType"))
            {
                this.winGridViewPager1.OnDeleteSelected += new EventHandler(winGridViewPager1_OnDeleteSelected);
            }
            this.winGridViewPager1.OnRefresh += new EventHandler(winGridViewPager1_OnRefresh);
            this.winGridViewPager1.OnAddNew += new EventHandler(winGridViewPager1_OnAddNew);
            this.winGridViewPager1.AppendedMenu = this.contextMenuStrip1;

            this.tsbNew.Enabled = Portal.gc.HasFunction("BasicInfo/ProductType");
            this.tsbEdit.Enabled = Portal.gc.HasFunction("BasicInfo/ProductType");
            this.tsbDelete.Enabled = Portal.gc.HasFunction("BasicInfo/ProductType");
        }

        private void winGridViewPager1_OnRefresh(object sender, EventArgs e)
        {
            BindData();
        }

        private void winGridViewPager1_OnDeleteSelected(object sender, EventArgs e)
        {
            if (MessageUtil.ShowYesNoAndTips("您确定删除选定的记录么？") == DialogResult.No)
            {
                return;
            }

            DataGridView grid = this.winGridViewPager1.dataGridView1;
            if (grid != null)
            {
                foreach (DataGridViewRow row in grid.SelectedRows)
                {
                    BLLFactory<ProductType>.Instance.Delete(row.Cells["ID"].Value.ToString());
                }
                BindData();
            }
        }

        private void winGridViewPager1_OnEditSelected(object sender, EventArgs e)
        {
            DataGridView grid = this.winGridViewPager1.dataGridView1;
            if (grid != null)
            {
                foreach (DataGridViewRow row in grid.SelectedRows)
                {
                    FrmEditProductType dlg = new FrmEditProductType();
                    dlg.ID = row.Cells["ID"].Value.ToString();
                    if (DialogResult.OK == dlg.ShowDialog())
                    {
                        BindData();
                    }

                    break;
                }
            }
        }

        private void winGridViewPager1_OnAddNew(object sender, EventArgs e)
        {
            FrmEditProductType dlg = new FrmEditProductType();
            if (DialogResult.OK == dlg.ShowDialog())
            {
                BindData();
            }
        }

        private void winGridViewPager1_OnStartExport(object sender, EventArgs e)
        {
            PagerInfo pagerInfo = new PagerInfo();
            pagerInfo.CurrenetPageIndex = 1;
            pagerInfo.PageSize = int.MaxValue;
            this.winGridViewPager1.AllToExport = BLLFactory<ProductType>.Instance.GetAllToDataSet(pagerInfo).Tables[0];//product.GetAllToDataSet(pagerInfo).Tables[0];
        }

        private void winGridViewPager1_OnPageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            #region 添加别名解析
            this.winGridViewPager1.AddColumnAlias("ID", "编号");
            this.winGridViewPager1.AddColumnAlias("Name", "类型名称");
            this.winGridViewPager1.AddColumnAlias("Note", "备注");
            this.winGridViewPager1.AddColumnAlias("LastUpdated", "更新日期");
            #endregion

            //product.GetAll(this.winGridViewPager1.PagerInfo);
            DataView collection = BLLFactory<ProductType>.Instance.GetAllToDataSet(this.winGridViewPager1.PagerInfo).Tables[0].DefaultView;
            this.winGridViewPager1.DataSource = collection;
        }

        private void tsbNew_Click(object sender, EventArgs e)
        {
            winGridViewPager1_OnAddNew(null, null);
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            winGridViewPager1_OnEditSelected(null, null);
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            winGridViewPager1_OnDeleteSelected(null, null);
        }

        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
